package com.beiing.tianshuai.tianshuai.dongtai.model;

import com.beiing.tianshuai.tianshuai.dongtai.CallBack;
import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicAddRelayCommentBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicAddReplyBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicCollectBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicCommentBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicDeleteBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicPraiseBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicShieldBean;
import com.beiing.tianshuai.tianshuai.entity.FollowBean;
import com.beiing.tianshuai.tianshuai.entity.FollowResultBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/7/17 0017.
 */

public class DynamicModel implements DynamicModelImpl {

    private static final String TAG = "DynamicModel";

    public DynamicModel() {
    }

    @Override
    public void getDynamicsResult(String id, int start, int num, String dynamicsId, String relayId, final CallBack callBack) {
        Observable<DynamicBean> loginResult = DataManager.getDynamicsResult(
                id,
                start,
                num,
                dynamicsId,
                relayId
        );
        Observable<DynamicBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<DynamicBean>() {

            @Override
            public void onNext(@NonNull DynamicBean bean) {
                callBack.result(bean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void praiseDynamic(String userid, String aid, String to_uid, final CallBack callBack) {
        DisposableObserver<DynamicPraiseBean> observer = new DisposableObserver<DynamicPraiseBean>() {
            @Override
            public void onNext(@NonNull DynamicPraiseBean dynamicPraiseBean) {
                callBack.result(dynamicPraiseBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getDynamicPraiseResult(userid, aid, to_uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void praiseRelay(String uid, String rid, String did, String to_uid, final CallBack callBack) {
        DisposableObserver<DynamicPraiseBean> observer = new DisposableObserver<DynamicPraiseBean>() {
            @Override
            public void onNext(@NonNull DynamicPraiseBean dynamicPraiseBean) {
                callBack.result(dynamicPraiseBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getRelayPraiseResult(uid, rid, did,to_uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void shieldDynamic(String uid, String did, final CallBack callBack) {
        DisposableObserver<DynamicShieldBean> observer = new DisposableObserver<DynamicShieldBean>() {
            @Override
            public void onNext(@NonNull DynamicShieldBean dynamicShieldBean) {
                callBack.result(dynamicShieldBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getShieldResult(uid, did)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void collectDynamic(String uid, String did, final CallBack callBack) {
        DisposableObserver<DynamicCollectBean> observer = new DisposableObserver<DynamicCollectBean>() {
            @Override
            public void onNext(@NonNull DynamicCollectBean dynamicCollectBean) {
                callBack.result(dynamicCollectBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getDynamicCollectResult(uid, did)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void deleteDynamic(String uid, String did, final CallBack callBack) {
        DisposableObserver<CommonBean> observer = new DisposableObserver<CommonBean>() {
            @Override
            public void onNext(@NonNull CommonBean dynamicDeleteBean) {
                callBack.result(dynamicDeleteBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getDynamicDeleteResult(uid, did)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void deleteRelay(String uid, String rid, final CallBack callBack) {
        DisposableObserver<CommonBean> observer = new DisposableObserver<CommonBean>() {
            @Override
            public void onNext(@NonNull CommonBean relayDelBean) {
                callBack.result(relayDelBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getRelayDelResult(uid, rid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void addComment(String uid, String to_uid, String aid, String content, String register_id, String create_time, final CallBack callBack) {
        DisposableObserver<DynamicCommentBean> observer = new DisposableObserver<DynamicCommentBean>() {
            @Override
            public void onNext(DynamicCommentBean dynamicCommentBean) {
                callBack.result(dynamicCommentBean);
            }

            @Override
            public void onError(Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };

        DataManager.getDynamicCommentResult(uid, to_uid, aid, content, register_id, create_time)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void addRelayComment(String rid, String uid, String did, String to_uid, String content, final CallBack callBack) {
        DisposableObserver<DynamicAddRelayCommentBean> observer = new DisposableObserver<DynamicAddRelayCommentBean>() {
            @Override
            public void onNext(DynamicAddRelayCommentBean dynamicAddRelayCommentBean) {
                callBack.result(dynamicAddRelayCommentBean);
            }

            @Override
            public void onError(Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };

        DataManager.getAddRelayCommentResult(rid, uid, did, to_uid, content)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void addReply(String did, String cid, String form_uid, String to_uid, String content, String registration_id, final CallBack callBack) {
        DisposableObserver<DynamicAddReplyBean> observer = new DisposableObserver<DynamicAddReplyBean>() {
            @Override
            public void onNext(@NonNull DynamicAddReplyBean dynamicAddReplyBean) {
                callBack.result(dynamicAddReplyBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getDynamicAddReplyResult(did, cid, form_uid, to_uid, content, registration_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void addRelayReply(String rid, String cid, String form_id, String to_uid, String content, String registration_id, final CallBack callBack) {
        DisposableObserver<DynamicAddReplyBean> observer = new DisposableObserver<DynamicAddReplyBean>() {
            @Override
            public void onNext(@NonNull DynamicAddReplyBean dynamicAddReplyBean) {
                callBack.result(dynamicAddReplyBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getAddRelayReplyResult(rid, cid, form_id, to_uid, content, registration_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void isFollowed(String former_uid, String latter_uid, final CallBack callBack) {
        DisposableObserver<FollowBean> observer = new DisposableObserver<FollowBean>() {
            @Override
            public void onNext(FollowBean followBean) {
                callBack.result(followBean);
            }

            @Override
            public void onError(Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.isFollowed(former_uid, latter_uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void setFollowed(String former_uid, String latter_uid, final CallBack callBack) {
        DisposableObserver<FollowResultBean> observer = new DisposableObserver<FollowResultBean>() {
            @Override
            public void onNext(FollowResultBean followResultBean) {
                callBack.result(followResultBean);
            }

            @Override
            public void onError(Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getFollowResult(former_uid, latter_uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void sendPush(String status, int tag, int id, String type, final CallBack callBack) {
        DisposableObserver<DynamicDeleteBean> observer = new DisposableObserver<DynamicDeleteBean>() {
            @Override
            public void onNext(DynamicDeleteBean deleteBean) {
                callBack.result(deleteBean);
            }

            @Override
            public void onError(Throwable e) {
                callBack.error(e);
            }

            @Override
            public void onComplete() {

            }
        };

        DataManager.getPushResult(status, tag, id, type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}
